<?php
/**
 * 前台登录 退出操作
 *
 *
 * @好商城 (c) 2015-2018 33HAO Inc. (http://www.33hao.com)
 * @license    http://www.33 hao.c om
 * @link       交流群号：138182377
 * @since      好商城提供技术支持 授权请购买shopnc授权
 */


defined('In33hao') or exit('Access Invalid!');

class loginControl extends mobileHomeControl
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 登录
     */
    public function indexOp()
    {
        if (empty($_POST['username']) || empty($_POST['password']) || !in_array($_POST['client'], $this->client_type_array)) {
            output_error('登录失败');
        }

        $model_member = Model('member');

        $login_info = array();
        $login_info['user_name'] = $_POST['username'];
        $login_info['password'] = $_POST['password'];

        $member_info = $model_member->login($login_info);

        if (isset($member_info['error'])) {
            output_error($member_info['error']);
        } else {
            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);
            $store = Model('store')->getStoreInfo(array('member_id' => $member_info['member_id']));
            if ($_POST['type'] == 2){
                if ($store['store_class']!=1) {
                    output_error('登录失败');
                    die;
                }
            }
            if ($token) {
                $_SESSION['member_id'] = $member_info['member_id'];
                $_SESSION['is_login'] = 1;
                if ($store) {
                    $_SESSION['store_id'] = $store['store_id'];
                }
                $yg_token = '';
                if ($_POST['type']==1) {
                    $yg_token = 'auth/'.$member_info['member_id'].'/'.$_POST['type'].'/'.md5($member_info['member_id'].'_'.date('Y-m-d H')).'/2';
                }
                if($_POST['type']==2){
                    output_data(array('username' => $member_info['member_name'], 'userid' => $member_info['member_id'], 'key' => $token,'store_id'=>$store['store_id']));
                    die;
                }
                output_data(array('username' => $member_info['member_name'], 'userid' => $member_info['member_id'], 'key' => $token,'token'=>$yg_token));
            } else {
                output_error('登录失败');
            }
        }
    }

    /**
     * 商家登录
     */
    Public function selle_loginOp()
    {
        if (empty($_POST['username']) || empty($_POST['password']) || !in_array($_POST['client'], $this->client_type_array)) {
            output_error('登录失败');
        }
        $model_seller = Model('seller');
        $seller_info = $model_seller->getSellerInfo(array('seller_name' => $_POST['username']));
        if ($seller_info) {
            $model_member = Model('member');

            $login_info = array();
            $login_info['user_name'] = $_POST['username'];
            $login_info['password'] = $_POST['password'];

            $member_info = $model_member->login($login_info);

            if (isset($member_info['error'])) {
                output_error($member_info['error']);
            } else {
                $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);
                if ($token) {
                    $_SESSION['member_id'] = $member_info['member_id'];    
                    $_SESSION['is_login'] = 1;
                    output_data(array('username' => $member_info['member_name'], 'userid' => $member_info['member_id'], 'key' => $token));
                } else {
                    output_error('登录失败');
                }
            }
        } else {
            output_error('登录失败');
        }
    }

    /**
     * 登录生成token
     */
    private function _get_token($member_id, $member_name, $client)
    {
        $model_mb_user_token = Model('mb_user_token');

        //重新登录后以前的令牌失效
        //暂时停用
        //$condition = array();
        //$condition['member_id'] = $member_id;
        //$condition['client_type'] = $client;
        //$model_mb_user_token->delMbUserToken($condition);

        //生成新的token
        $mb_user_token_info = array();
        $token = md5($member_name . strval(TIMESTAMP) . strval(rand(0, 999999)));
        $mb_user_token_info['member_id'] = $member_id;
        $mb_user_token_info['member_name'] = $member_name;
        $mb_user_token_info['token'] = $token;
        $mb_user_token_info['login_time'] = TIMESTAMP;
        $mb_user_token_info['client_type'] = $client;

        $result = $model_mb_user_token->addMbUserToken($mb_user_token_info);

        if ($result) {
            return $token;
        } else {
            return null;
        }

    }

    /**
     * 注册
     */
    public function registerOp()
    {
        $model_member = Model('member');

        $register_info = array();
        $register_info['username'] = $_POST['username'];
        $register_info['password'] = $_POST['password'];
        $register_info['password_confirm'] = $_POST['password_confirm'];
        $register_info['member_provinceid'] = $_POST['province_id'];
        $register_info['member_cityid'] = $_POST['city_id'];
        $register_info['member_areaid'] = $_POST['area_id'];
        $register_info['member_areainfo'] = $_POST['area_info'];
        $register_info['email'] = $_POST['email'];
        $uid = intval(base64_decode($_COOKIE['uid'])) / 1;
        //关联推荐人
        if ($uid) {
            $register_info['inviter_id'] = $uid;
            $inviteLev = $this->QueryinviterLevel($uid);
            $register_info['Lev1'] = $uid;
            $register_info['Lev2'] = isset($inviteLev['Lev2']) ? $inviteLev['Lev2'] : 0;
            $register_info['Lev3'] = isset($inviteLev['Lev3']) ? $inviteLev['Lev3'] : 0;
        }
        // 店铺扫码推荐
        $invite_store_id = $_COOKIE['invite_store_id'];
        if ($invite_store_id) {
            $store_info = Model('store')->getStoreInfo(array('store_id' => $invite_store_id));
            $inviteLev = $this->QueryinviterLevel($store_info['member_id']);
            $register_info['inviter_id'] = $store_info['member_id'];
            $register_info['Lev1'] =  $store_info['member_id'];
            $register_info['Lev2'] = isset($inviteLev['Lev2']) ? $inviteLev['Lev2'] : 0;
            $register_info['Lev3'] = isset($inviteLev['Lev3']) ? $inviteLev['Lev3'] : 0;
        }
        $member_info = $model_member->register($register_info);
        $store = Model('store')->getStoreInfo(array('member_id' => $member_info['member_id']));
        if ($store) {
            $_SESSION['store_id'] = $store['store_id'];
        }
        if (!isset($member_info['error'])) {
            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);
            if ($token) {
                $yg_token = '';
                if ($_POST['type']) {
                    $yg_token = 'auth/'.$member_info['member_id'].'/'.$_POST['type'].'/'.md5($member_info['member_id'].'_'.date('Y-m-d H')).'/2';
                }
                output_data(array('username' => $member_info['member_name'], 'userid' => $member_info['member_id'], 'key' => $token,'token'=>$yg_token));
            } else {
                output_error('注册失败');
            }
        } else {
            output_error($member_info['error']);
        }
    }

    /**
     * 查询邀请人层级
     * @param $uid 邀请人id
     */
    public function QueryinviterLevel($uid)
    {
        $inviterLevel = array();
        $model_member = Model('member');
        $condition['member_id'] = $uid;
        $memer_info = $model_member->getMemberInfo($condition);
//        if (empty($memer_info['inviter_id'])) {
//            $inviterLevel['Lev1'] = $uid;
//            return $inviterLevel;
//            exit();
//        } else {
//
//            $inviterLevel['Lev1'] = $uid;
//            $inviterLevel['Lev2'] = $memer_info['inviter_id'];
//        }
//        $condition2['member_id'] = $memer_info['inviter_id'];
//        $memer_info1 = $model_member->getMemberInfo($condition2);
//        if (empty($memer_info1['inviter_id'])) {
//            return $inviterLevel;
//            exit();
//        } else {
//            $inviterLevel['Lev1'] = $uid;
//            $inviterLevel['Lev2'] = $memer_info['inviter_id'];
//            $inviterLevel['Lev3'] = $memer_info1['member_id'];
//        }
        $inviterLevel['Lev1'] = $uid;
        $inviterLevel['Lev2'] = $memer_info['lev1'];
        $inviterLevel['Lev3'] = $memer_info['lev2'];
        return $inviterLevel;
    }
}
